﻿using FlowCard.PrintCard.Models;
using FlowCard.PrintCard.Services;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Options;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;

namespace FlowCard.PrintCard
{
    public static class FlowCardExtensions
    {
        /// <summary>
        /// IOC注入流程卡片
        /// </summary>
        /// <param name="services"></param>
        /// <param name="configuration"></param>
        /// <returns></returns>
        public static IServiceCollection AddFlowCard(this IServiceCollection services, IConfiguration configuration)
        {
            services.AddFlowCardConfig(configuration);
            services.AddScoped<IGenerateFile, GenerateFile>();
            return services;
        }
        /// <summary>
        /// 配置文件注入IOC
        /// </summary>
        /// <param name="services"></param>
        /// <param name="configuration"></param>
        /// <returns></returns>
        /// <exception cref="ArgumentNullException"></exception>
        private static IServiceCollection AddFlowCardConfig(this IServiceCollection services, IConfiguration configuration)
        {
            //services.Configure<ConstConfig>(configuration.GetSection("FlowCard").Bind);

            var config = configuration.GetSection("FlowCard").Get<ConstConfig>() ?? new ConstConfig();
            var currentDirectory = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);

            if (currentDirectory == null) { throw new ArgumentNullException(nameof(currentDirectory)); }

            var configCopy = new ConstConfig()
            {
                PrintTempPdf = config.PrintTempPdf ?? Path.Combine(currentDirectory, "FlowCardFile", "PrintTempPdf.xlsx"),
                PrintTempExcel = config.PrintTempExcel ?? Path.Combine(currentDirectory, "FlowCardFile", "PrintTempExcel.xlsx"),
                PrintTempPdfTwo = config.PrintTempPdfTwo ?? Path.Combine(currentDirectory, "FlowCardFile", "PrintTempPdfTwo.xlsx"),
                PrintTempExcelTwo = config.PrintTempExcelTwo ?? Path.Combine(currentDirectory, "FlowCardFile", "PrintTempExcelTwo.xlsx"),
                ImagePath = config.ImagePath ?? Path.Combine(currentDirectory, "FlowCardFile", "ImageTemplate.png"),
                PathPdf = config.PathPdf ?? Path.Combine(currentDirectory, "FlowCardFile", "TempPdfFile\\"),
                PathExcel = config.PathExcel ?? Path.Combine(currentDirectory, "FlowCardFile", "TempExcelFile\\"),
                PathPdfTwo = config.PathPdfTwo ?? Path.Combine(currentDirectory, "FlowCardFile", "TempPdfFileTwo\\"),
                PathExcelTwo = config.PathExcelTwo ?? Path.Combine(currentDirectory, "FlowCardFile", "TempExcelFileTwo\\"),
            };
            var configCopy1 = new ConstConfig()
            {
                PrintTempPdf = configCopy.PrintTempPdf.StartsWith("\\") ? $"{currentDirectory}{configCopy.PrintTempPdf}" : configCopy.PrintTempPdf,
                PrintTempExcel = configCopy.PrintTempExcel.StartsWith("\\") ? $"{currentDirectory}{configCopy.PrintTempExcel}" : configCopy.PrintTempExcel,
                PrintTempPdfTwo = configCopy.PrintTempPdfTwo.StartsWith("\\") ? $"{currentDirectory}{configCopy.PrintTempPdfTwo}" : configCopy.PrintTempPdfTwo,
                PrintTempExcelTwo = configCopy.PrintTempExcelTwo.StartsWith("\\") ? $"{currentDirectory}{configCopy.PrintTempExcelTwo}" : configCopy.PrintTempExcelTwo,
                ImagePath = configCopy.ImagePath.StartsWith("\\") ? $"{currentDirectory}{configCopy.ImagePath}" : configCopy.ImagePath,
                PathPdf = configCopy.PathPdf.StartsWith("\\") ? $"{currentDirectory}{configCopy.PathPdf}" : configCopy.PathPdf,
                PathExcel = configCopy.PathExcel.StartsWith("\\") ? $"{currentDirectory}{configCopy.PathExcel}" : configCopy.PathExcel,
                PathPdfTwo = configCopy.PathPdfTwo.StartsWith("\\") ? $"{currentDirectory}{configCopy.PathPdfTwo}" : configCopy.PathPdfTwo,
                PathExcelTwo = configCopy.PathExcelTwo.StartsWith("\\") ? $"{currentDirectory}{configCopy.PathExcelTwo}" : configCopy.PathExcelTwo,
            };

            services.AddSingleton<ConstConfig>(configCopy1);
            return services;
        }
    }
}
